/*
 * Created by: vadikv
 * Created: Monday, April 07, 2008
 */

using System.Collections.Generic;
using OneDayModel.Interfaces;

namespace OneDayModel.Model
{
    public class ExistingUserProfileModel : IExistingUserProfileModel
    {
        private readonly IRepository repository;

        public ExistingUserProfileModel(IRepository repository)
        {
            this.repository = repository;
        }

        public ExistingUserProfileData GetProfileData(string userAlias)
        {
            IUser user = repository.GetUserByAlias(userAlias);
            return new ExistingUserProfileData(GetUserRoles(user), GetAttributeRecords(user));
        }

        #region IExistingUserProfileModel Members

        public IDictionary<string, IRoleAttribute> AllAttributesDefinitions
        {
            get { return repository.AllAttributesDefinitions; }
        }

        #endregion

        private IEnumerable<KeyValuePair<string, string>> GetAttributeRecords(IUser user)
        {
            return user.AttributesRecords;
        }

        private IEnumerable<string> GetUserRoles(IUser user)
        {
            foreach (IRole role in user.Roles)
            {
                yield return role.Name;
            }
        }
    }
}